<template>
	<view class="mainBg h100 ">
		<view class="container">
			<view class="logo">
				<image src="@/static/logo.svg" mode=""></image>
				<view class="">
					妙有芯世界
				</view>
			</view>
			<view class="form" v-if="isCode">
				<view class="col">
					<image src="@/static/login/form-icon1.png" mode=""></image>
					<input v-model="phone" type="tel" placeholder="请输入手机号" />
				</view>
				<view class="col">
					<image src="@/static/login/form-icon2.png" mode=""></image>
					<input v-model="code" type="text" placeholder="请输入验证码" />
					<sendCode :phone="phone" :event="event" />
				</view>
			</view>
			<view class="form" v-else>
				<view class="col">
					<image src="@/static/login/form-icon3.png" mode=""></image>
					<input v-model="phone" type="text" placeholder="请输入名号" />
				</view>
				<view class="col">
					<image src="@/static/login/form-icon2.png" mode=""></image>
					<input v-model="password" type="password" placeholder="请输入密码" />
				</view>
			</view>
			<view class="loginBtn">
				<text v-if="isCode" @click="isCode=false">密码登录</text>
				<text v-else @click="isCode=true">验证码登录</text>
				<text>
					<navigator url="/pages/login/signup">注册名号</navigator>
				</text>
			</view>
			<view class="submit" @click="submit">
				登录
			</view>
		</view>
		<!-- <view class="auth">
			<text>第三方授权登录</text>
			<view class="icon">
				<image src="@/static/login/auth-icon1.png" mode=""></image>
				<image src="@/static/login/auth-icon2.png" mode=""></image>
			</view>
		</view> -->
	</view>
</template>

<script>
	import sendCode from '@/components/sendCode.vue'
	export default {
		components: {
			sendCode
		},
		data() {
			return {
				isCode: true,
				phone: '',
				code: '',
				password: '',
				event:'login'
			}
		},
		methods: {
			submit() {
				if (this.isCode) {
					this.codeLogin()
				} else {
					this.pswLogin()
				}
			},
			pswLogin() {
				if(!this.phone)
				{
					this.$toast("请输入手机号")
					return false
				}
				if(!this.password)
				{
					this.$toast("请输入密码")
					return false
				}
				this.$loading("登录中...")
				this.$post("/user/login", {
					phone: this.phone,
					password: this.password
				}).then(res => {
					if(res.code == 200)
					{
						uni.setStorage({
							data: res.data.token,
							key: "token"
						})
						// uni.switchTab({
						// 	url: "/pages/index/index"
						// })
						uni.navigateTo({
											url: "/pages/login/check"
										})
					}else{
						this.$toast(res.msg)
						return false
					}
					
				})
			},
			codeLogin() {
				if(!this.phone)
				{
					this.$toast("请输入手机号")
					return false
				}
				if(!this.code)
				{
					this.$toast("请输入验证码")
					return false
				}
				this.$loading("登录中...")
				this.$post("/user/codeLogin", {
					phone: this.phone,
					code: this.code
				}).then(res => {
					if(res.code == 200)
					{
						uni.setStorage({
							data: res.data.token,
							key: "token"
						})
						// uni.switchTab({
						// 	url: "/pages/index/index"
						// })
						uni.navigateTo({
											url: "/pages/login/check"
										})
					}else{
						this.$toast(res.msg)
						return false
					}
					
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #35CBAD;
	}

	.logo {
		text-align: center;
		color: #fff;
		font-size: 40rpx;
		font-weight: bold;
		margin-top: 180rpx;

		image {
			width: 104rpx;
			height: 104rpx;
		}
	}

	.form {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 0 40rpx;
		margin-top: 50rpx;

		.col {
			display: flex;
			align-items: center;
			padding: 40rpx 0;
			border-bottom: 1px solid #eee;
			position: relative;

			image {
				width: 36rpx;
				height: 36rpx;
			}

			input {
				font-size: 28rpx;
				padding: 0 20rpx;
			}
		}
	}

	.submit {
		background: linear-gradient(180deg, #4095E5, #51C76C);
		text-align: center;
		border-radius: 100px;
		color: #fff;
		line-height: 82rpx;
		font-size: 32rpx;
		margin-top: 80rpx;
	}

	.loginBtn {
		margin-top: 50rpx;
		display: flex;
		justify-content: space-between;

		text {
			color: #fff;
			font-size: 28rpx;
		}
	}

	.auth {
		margin-top: 150rpx;
		text-align: center;

		text {
			position: relative;
			color: #fff;
			font-size: 40rpx;
			font-weight: bold;

			&::before,
			&::after {
				content: "";
				width: 100%;
				height: 2rpx;
				background-color: #fff;
				position: absolute;
				top: 50%;
			}

			&::before {
				left: -100%;
				transform: translateX(-20rpx);
			}

			&::after {
				left: 100%;
				transform: translateX(20rpx);
			}
		}

		.icon {
			margin-top: 80rpx;

			image {
				width: 90rpx;
				height: 90rpx;
				display: inline-block;
				margin: 0 50rpx;
			}
		}
	}
</style>